trigger AccountTrigger on Account (before insert, before update, before delete) 
{
    Account oldObj;
    Account newObj;
    ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Account', 'Local OOR Customer');
    if(Trigger.isBefore && Trigger.isInsert)
        NationalAccountEvents.OnAccountBeforeInsert(Trigger.old, Trigger.new);
        
    if(Trigger.isBefore && Trigger.isUpdate)
        NationalAccountEvents.OnAccountBeforeUpdate(Trigger.old, Trigger.new);

    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)) 
    {       
        for (Integer i = 0; i < Trigger.new.size(); i++)        
        {
            oldObj = Trigger.isUpdate ? Trigger.old[i] : null;
            newObj = Trigger.new[i];

                        
            if (localAdvertiserRecordTypeID == newObj.RecordTypeId)
            {
                if (Trigger.isInsert || (Trigger.isUpdate && oldObj.Phone != newObj.Phone)) // Disable this line for first time inital load 
                   newObj.CleanPhoneText__c = SilCustomerHelper.CreatePhoneNumberHash(newObj.Phone);
                   
                //prevent the charges fields update to zero by SIL--this is a temp fix that needs to be removed soon! -Sree
                if (Trigger.isUpdate && newObj.Recurring_Charge__c == 0) 
                    newObj.Recurring_Charge__c = oldObj.Recurring_Charge__c;
                if (Trigger.isUpdate && newObj.Onetime_Charge_OC__c== 0) 
                    newObj.Onetime_Charge_OC__c= oldObj.Onetime_Charge_OC__c;
                    
                ID defaultOORAccountOwner = SilConfiguration__c.getInstance().LA_AccountOwner__c;
               
                string newOwner = newObj.ownerId;
                System.Debug('sree'+defaultOORAccountOwner + ' =' +   newOwner);  
                //Revert owner update if it is an update to OOR account and the owner is inactive and new owner is Exception user!
                if (Trigger.isUpdate && newOwner.Contains(defaultOORAccountOwner) && oldObj.owner.IsActive == false) {
                   newObj.ownerId = oldObj.ownerId;
                   System.Debug('sree');
                   }

            }
        }   
    }
    
    if(Trigger.isBefore && Trigger.isDelete) 
    {       
        for (Integer i = 0; i < Trigger.old.size(); i++)        
        {
            oldObj = Trigger.old[i];
            
            if(oldObj.Is_API_Controlled__c && !userinfo.getUserName().startsWith('sadmin@ingenio.com'))
                oldObj.addError('API Controlled Accounts cannot be deleted.');
        }
    }
}